package com.rpframework.module.common.bottom.api.juhe.cn;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rpframework.module.common.bottom.entity.po.Geography;
import com.rpframework.utils.OkHttpUtil;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;

/**
 * 位置
 * <p>
 * JSON:
 * {
 * "resultcode": "1",
 * "resultinfo": "Successful",
 * "row": {
 * "status": "OK",
 * "result": {
 * "location": {
 * "lng": 116.407431,
 * "lat": 39.914492
 * },
 * "formatted_address": "北京市东城区东长安街",
 * "business": "天安门,前门,大栅栏",
 * "addressComponent": {
 * "city": "北京市",
 * "direction": "",
 * "distance": "",
 * "district": "东城区",
 * "province": "北京市",
 * "street": "东长安街",
 * "street_number": ""
 * },
 * "cityCode": 131
 * }
 * }
 * }
 * <p>
 * Created by jump on 2016/8/9.
 */
public class Position {
    /**
     * 街道地址
     */
    private String formattedAddress;

    private String business;
    /**
     * CODE
     */
    private Integer cityCode;

    private AddressComponent addressComponent;

    private Location location;

    public String getFormattedAddress() {
        return formattedAddress;
    }

    public void setFormattedAddress(String formattedAddress) {
        this.formattedAddress = formattedAddress;
    }

    public String getBusiness() {
        return business;
    }

    public void setBusiness(String business) {
        this.business = business;
    }

    public Integer getCityCode() {
        return cityCode;
    }

    public void setCityCode(Integer cityCode) {
        this.cityCode = cityCode;
    }

    public AddressComponent getAddressComponent() {
        return addressComponent;
    }

    public void setAddressComponent(AddressComponent addressComponent) {
        this.addressComponent = addressComponent;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public class AddressComponent {
        private String city;
        private String direction;
        private String distance;
        private String district;
        private String province;
        private String street;
        private String streetNumber;

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getDirection() {
            return direction;
        }

        public void setDirection(String direction) {
            this.direction = direction;
        }

        public String getDistance() {
            return distance;
        }

        public void setDistance(String distance) {
            this.distance = distance;
        }

        public String getDistrict() {
            return district;
        }

        public void setDistrict(String district) {
            this.district = district;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getStreet() {
            return street;
        }

        public void setStreet(String street) {
            this.street = street;
        }

        public String getStreetNumber() {
            return streetNumber;
        }

        public void setStreetNumber(String streetNumber) {
            this.streetNumber = streetNumber;
        }
    }

    public class Location {
        private Double lng;
        private Double lat;

        public Double getLng() {
            return lng;
        }

        public void setLng(Double lng) {
            this.lng = lng;
        }

        public Double getLat() {
            return lat;
        }

        public void setLat(Double lat) {
            this.lat = lat;
        }
    }

    /**
     * 根据经纬度获取详细信息
     *
     * @param geography 经纬度
     * @return 信息
     */
    @Deprecated //接口已404
    public static Position getPosition(Geography geography) {
        Geography.baseValidate(geography);
        ArrayList<BasicNameValuePair> basicNameValuePairs = new ArrayList<>();
        basicNameValuePairs.add(new BasicNameValuePair("lngx", geography.getLatitude().toString()));
        basicNameValuePairs.add(new BasicNameValuePair("lngy", geography.getLongitude().toString()));
        String post;
        try {
            post = OkHttpUtil.post(Config.getUrl("/getaddressbylngb"), basicNameValuePairs);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Gson gson = new Gson();
        java.lang.reflect.Type type = new TypeToken<Json<Position>>() {
        }.getType();
        Json<Position> json = gson.fromJson(post, type);
        Json.success(json);
        return json.getRow().getResult();
    }
}
